// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-07-24

package middlewares

import (
	"encoding/json"
	"fmt"
	"gitee.com/go-libs/log"
	"github.com/kataras/iris/v12"
	"net/http"
	"regexp"
	"strings"
)

const (
	TracingSpan    = "__OpenTelemetrySpan__"
	TracingTraceId = "X-Request-Id"
)

const (
	tracingContentTypeUrlEncoded = "application/x-www-form-urlencoded"
	tracingContentTypeUrlJson    = "application/json"
	tracingContentTypeUrlXml     = "application/xml"
)

var (
	regexTracingMatchContentType   = regexp.MustCompile(`^([a-zA-Z]+/[_a-zA-Z0-9-]+)`)
	regexTracingRemovePayloadSpace = regexp.MustCompile(`\s*\n+\s*`)
)

// Tracing
// build OpenTelemetry around each http request that compatible
// with OpenTracing. It's a top level middleware of all middleware.
func Tracing(i iris.Context) {
	// Creates
	// a span with `gitee.com/go-libs/log` package.
	span := log.NewSpanWithRequest(i.Request())

	// Prepare
	// request begin logging message for request method, uri and headers.
	buf, _ := json.Marshal(i.Request().Header)
	msg := fmt.Sprintf(`request begin: method="%s", uri="%s", headers=%s`,
		i.Method(),
		i.Request().RequestURI,
		buf,
	)

	// Append
	// request body to logging message if allowed.
	//
	// Supports:
	//   - application/json
	//   - application/xml
	//   - application/x-www-form-urlencoded
	if i.Request().Method == http.MethodPost {
		if k := i.Request().Header.Get("Content-Type"); k != "" {
			if m := regexTracingMatchContentType.FindStringSubmatch(k); len(m) > 0 {
				switch strings.ToLower(m[1]) {
				case tracingContentTypeUrlEncoded,
					tracingContentTypeUrlJson,
					tracingContentTypeUrlXml:
					{
						if b, be := i.GetBody(); be == nil {
							msg += fmt.Sprintf(`, payload=%s`,
								regexTracingRemovePayloadSpace.ReplaceAll(b, nil),
							)
						}
					}
				}
			}
		}
	}

	// Logger
	// request message.
	log.Infofc(span.GetContext(), "%s", msg)

	// Execute
	// when request end.
	defer func() {
		// Logger
		// end message if enabled.
		if log.Config().DebugOn() {
			log.Debugfc(span.GetContext(), `request finish`)
		}

		// Close
		// request span.
		span.End()
	}()

	// Append
	// request id on response header.
	i.ResponseWriter().Header().Set(TracingTraceId, span.GetTracing().GetTraceId().String())

	// Append
	// span reference on iris context, You can get it by i.Values().Get(framework.TracingSpan).
	i.Values().Set(TracingSpan, span)

	// Continue
	// next middlewares.
	i.Next()
}
